#!/usr/bin/perl
$home="./";
$tunnet=$home."tunnet";
$nbr_file=$home."neighbors.conf";
$srvc_file=$home."services.conf";
$rt_file=$home."routes.conf";
$log_file=$home."info.log";
$access_file=$home."access.log";
$pid_file=$home."pid.log";


#$cmd_start=$tunnet." -n $nbr_file -s $srvc_file -r $rt_file -d ";
$cmd_start=$tunnet."  -n $nbr_file -s $srvc_file -r $rt_file  -d -l $log_file -L $access_file -p $pid_file" ;

$cmd_check="ps `cat $pid_file` >/dev/null";
$cmd_stop="kill `cat $pid_file` >/dev/null";



if($ARGV[0] eq "status"){

	my $status=&status_check();
	if($status) {
                print "tunnet is running \n";
        }else{
                print "tunnet is not running \n";
        }
	
	exit(0);
}
elsif($ARGV[0] eq "start"){

	my $status=&status_check();
	if($status) {
		exit(0);
	}
	else{
		#print $cmd_start;
 		my $res=system($cmd_start);
		#print $res;
	}

}
elsif($ARGV[0] eq "stop"){
	my $status=&status_check();
	if(!$status) {
		exit(0);
	}
	else{
 		my $res=system($cmd_stop);
	}
}
else{
	&usage();
}

sub usage{
	print "Usage: $0 start|status|stop\n";
	exit -1;
}

sub status_check{
	if(! -e $pid_file) {
		return 0; 
	}
        my $res=system($cmd_check);
        if(!$res) {
		return 1;
        }else{
		return 0;
        }
}
